<template>
  <div class="header-query-li">
    <div class="query-label" :style="borderStyle">{{ query.label }}</div>
    <div class="label-li" :style="borderStyle" @click="callBack">
      <span :class="active === null ? 'active' : ''" :style="borderStyle">全部</span>
    </div>
    <div
      v-for="(item, index) in dataList"
      :key="'label-' + index"
      class="label-li"
      :style="borderStyle"
      @click="callBack(item)"
    >
      <span :style="borderStyle" :class="active === item.value ? 'active' : ''">{{
        item.name
      }}</span>
    </div>
  </div>
</template>

<script>
import { queryAllServiceParamsLabel } from '@/api/portal/manager/setting/index'
import { layout } from '@/mixins/layout'
export default {
  name: 'OfficeHallItem', // 办事大厅分类模块：主管部门、办事方式、服务对象部分
  mixins: [layout],
  props: {
    params: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      query: {},
      dataList: [],
      active: null
    }
  },
  computed: {
    themes() {
      return this.$store.getters.theme
    },
    borderStyle() {
      return {
        '--label-color': this.layoutTheme.color,
        '--label-background': this.layoutTheme.labelBackground,
        '--label-active-color': this.layoutTheme.labelActiveColor,
        '--label-active-background': this.layoutTheme.labelActiveBackground
      }
    }
  },
  watch: {
    params: {
      handler(newData, oldData) {
        this.active = null
        this.query = newData
        this.initData({ code: newData.column })
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    initData(data) {
      queryAllServiceParamsLabel(data)
        .then(res => {
          const { data } = res
          this.dataList = data
        })
        .catch(() => {
          this.dataList = []
        })
    },
    callBack(data) {
      if (
        undefined !== data &&
        undefined !== data.value &&
        data.value !== null
      ) {
        this.active = data.value
        this.$emit('call-back', { code: this.query.column, value: data.value })
      } else {
        this.active = null
        this.$emit('call-back', { code: this.query.column, value: null })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.header-query-li {
  min-height: 50px;
  border-bottom: 1px dashed #efefef;
  display: flex;
  flex-flow: row wrap;
}
.query-label {
  flex: 0 1 auto;
  width: 100px;
  text-align: center;
  line-height: 50px;
  font-weight: bolder;
  color: var(--label-color);
}
.label-li {
  flex: 0 1 auto;
  line-height: 50px;
  margin-left: 5px;
}
.label-li span {
  border-radius: 3px;
  cursor: pointer;
  padding: 5px 10px;
  box-sizing: border-box;
  background-color: var(--label-background);
  color: var(--label-color);
}
span.active,
.label-li span:hover {
  background-color: var(--label-active-background);
  color: var(--label-active-color);
}
</style>
